package jpavlich.cardsorter;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import javax.swing.JComboBox;

public class NewExperimentDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtExper;
	private JComboBox cbCardSet;

	/**
	 * Create the dialog.
	 * 
	 * @param actionListener
	 */
	public NewExperimentDialog(ExperimentsListModel exps) {
		super((Frame) null, true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 252);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[grow]", "[][][][][]"));
		{
			JLabel lblIdentificadorDelExperimento = new JLabel(
					"Identificador del experimento:");
			contentPanel.add(lblIdentificadorDelExperimento, "cell 0 0");
		}
		{
			txtExper = new JTextField();
			txtExper.setText(exps.getNewExperimentID());
			contentPanel.add(txtExper, "cell 0 1,growx");
			txtExper.setColumns(10);
		}
		{
			JLabel lblSetDeCartas = new JLabel("Set de cartas");
			contentPanel.add(lblSetDeCartas, "cell 0 3");
		}
		{
			cbCardSet = new JComboBox();
			cbCardSet.setModel(new CardsListModel());
			contentPanel.add(cbCardSet, "cell 0 4,growx");
			if (cbCardSet.getModel().getSize() > 0) {
				cbCardSet.setSelectedIndex(0);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Controller.startNewExperiment(NewExperimentDialog.this,
								txtExper.getText());
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public JComboBox getCbCardSet() {
		return cbCardSet;
	}

}
